(function($){
    $.fn.serializeJson=function(){
        var serializeObj={};
        var array=this.serializeArray();
        // var str=this.serialize();
        $(array).each(function(){
            if(serializeObj[this.name]){
                if($.isArray(serializeObj[this.name])){
                    serializeObj[this.name].push(this.value);
                }else{
                    serializeObj[this.name]=[serializeObj[this.name],this.value];
                }
            }else{
                serializeObj[this.name]=this.value;
            }
        });
        return serializeObj;
    };
    init_validate();
})(jQuery);
function init_validate() {
    jQuery.validator.addMethod("phoneNumber", function(value, element) {
        var pnb = /^((0\d{2,3}-\d{7,8})|(1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}))$/;
        return this.optional(element) || (pnb.test(value));
    }, "请输入正确的电话号码");
    $("#travelAgencyForm").validate({
        rules: {
            linkPhone: {
                required: true,
                phoneNumber:true,
                rangelength: [5, 15]

        },
        messages: {

            linkPhone: {
                required: "联系电话是必填项目"
            }
        }
    }});
}

$(document).ready(function(){
    var ueditor = UE.getEditor('ueditor', {wordCount: true});
    if($('#imgDiv').length>0){
        $('#logoUploadFile').ace_file_input({
            style:'well',
            btn_choose:'请选择 jpg,jpeg,png 等格式的文件进行上传',
            btn_change:null,
            no_icon:'ace-icon fa fa-cloud-upload',
            droppable:false,
            allowExt: ['jpg','jpeg','png'],
            thumbnail:'small',
            preview_error : function(filename, error_code) {
            }

        }).on('change', function(){
            if (window.File && window.FileReader && window.FileList && window.Blob) {
                var file = $('#logoUploadFile').prop("files")[0];
                var form_data = new FormData();
                form_data.append("file[]", file);
                $.ajax({
                    type: "POST",
                    url: "/file/uploadFile",
                    dataType : "json",
                    processData: false,  // 注意：让jQuery不要处理数据
                    contentType: false,  // 注意：让jQuery不要设置contentType
                    data: form_data
                }).success(function(msg) {
                    $("#logo_url").val(msg.datas);
                    $("#logoImgId").attr('src',msg.datas);
                    // DrawImage($("#logoImgId").get(0),$("#mainBusiness").width(), 300);
                    jQuery("#imgDiv img").each(function () {
                        var img = this;
                        setTimeout(function () {
                            DrawImage(img,$("#mainBusiness").width(), 300);
                        },1000);
                        $('#logoUploadFile').p
                    });
                    $('#imgDiv').show();
                }).fail(function(msg) {
                    console.log(msg);
                });
            } else {
                alert('不支持');
            }
        }).on('file.error.ace', function(event, info) {
            bootbox.alert("请上传jpg,jpeg,png格式文件");
        });
    }

    $('#submitBtn').click(function (e) {
        if (!$("#travelAgencyForm").valid()) {
            return false;
        }
        e.preventDefault();
        var data=$("#travelAgencyForm").serializeJson();//表单序列化
        data.introduce = ueditor.getContent();
        jQuery.ajax({
            dataType : 'json',
            type:'post',
            url : $('#travelAgencyForm').attr('action'),
            data :data,
            success : function(result) {
                if(result.code=='200'){
                    bootbox.confirm({
                        title:'提示信息',
                        message: result.message+'是否继续添加？',
                        buttons: {
                            confirm: {
                                label: '是',
                                className: 'btn-success'
                            },
                            cancel: {
                                label: '否',
                                className: 'btn-default'
                            }
                        },
                        callback: function (result) {
                            if(result){
                                if($('input[name="id"]').val()==""){
                                    $('#travelAgencyForm')[0].reset();
                                    ueditor.setContent('');
                                }else{
                                    // var url = $('#addUrl').val();
                                    window.location.href="/travelagency/input.do";
                                }
                            }else{
                                window.location.href="/tour/route/talist.do";
                            }
                        }
                    });
                }else{
                    bootbox.alert(result.message);
                }
            },
            error:function () {
                bootbox.alert('请求失败！');
            }
        });
    });
    $('.datepicker').datepicker({
        todayBtn : "linked",
        autoclose : true,
        todayHighlight : true,
        format:'yyyy-mm-dd',
        language : 'zh-CN'
    });
    $('#resetBtn').click(function(e) {
        $('#travelAgencyForm')[0].reset();
    });
    jQuery("#imgDiv img").each(function () {
        DrawImage(this,$("#mainBusiness").width(), 300);
    });
});
function DrawImage(ImgD, FitWidth, FitHeight) {
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= FitWidth / FitHeight) {
            if (image.width > FitWidth) {
                ImgD.width = FitWidth;
                ImgD.height = (image.height * FitWidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        } else {
            if (image.height > FitHeight) {
                ImgD.height = FitHeight;
                ImgD.width = (image.width * FitHeight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }
}